import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../config/index.dart';

/// 获取当前主题资源
ThemeResources get themeResources => ThemeTool().currentThemeResources;

/// 获取当前主题颜色
ThemeColors get themeColors => themeResources.colors;

/// 获取当前主题图片
ThemeImages get themeImages => themeResources.images;

/// 是否是暗黑模式
bool get isDarkMode => themeResources.isDarkMode;

class ThemeTool {
  // 单例模式
  static final ThemeTool _instance = ThemeTool._internal();
  factory ThemeTool() => _instance;

  // 私有构造函数，初始值为亮色主题
  ThemeTool._internal();

  // 当前主题资源
  ThemeResources _currentThemeResources = ThemeResources.light;

  // 获取当前主题资源
  ThemeResources get currentThemeResources => _currentThemeResources;

  changeTheme() {
    toggleTheme(
      type: isDarkMode ? 0 : 1,
    );
  }

  // 切换主题
  void toggleTheme({
    int type = defaulThemeType,
    bool isUserCacha = true,
  }) async {
    ThemeMode themeMode = ThemeMode.system;
    if (type == 0) {
      themeMode = ThemeMode.light;
      setLightTheme();
    } else if (type == 1) {
      themeMode = ThemeMode.dark;
      setDarkTheme();
    } else {
      themeMode = ThemeMode.system;
      if (isDarkMode) {
        setLightTheme();
      } else {
        setDarkTheme();
      }
    }
    configThirdToolTheme(themeMode: themeMode);
    if (isUserCacha) {
      await SpConfig.setThemeType(type: type);
    }
    Get.changeThemeMode(themeMode);
  }

  /// 配置第三方工具的主题
  /// 比如：EasyLoading
  configThirdToolTheme({
    ThemeMode themeMode = ThemeMode.system,
  }) {
    EasyLoadingStyle easyLoadingStyle = EasyLoadingStyle.dark;
    if (themeMode == ThemeMode.dark) {
      easyLoadingStyle = EasyLoadingStyle.light;
    } else if (themeMode == ThemeMode.system) {
      if (!isDarkMode) {
        easyLoadingStyle = EasyLoadingStyle.light;
      }
    }
    EasyLoading.instance.loadingStyle = easyLoadingStyle;
  }

  // 设置为亮色主题
  void setLightTheme() {
    _currentThemeResources = ThemeResources.light;
  }

  // 设置为暗色主题
  void setDarkTheme() {
    _currentThemeResources = ThemeResources.dark;
  }

  /// 获取本地 主题配置
  /// 0 白色
  /// 1 黑色
  /// 2 跟随系统
  getLocalProfileThemeModel({
    bool isUserCache = true,
    int themeType = defaulThemeType,
  }) {
    int type = isUserCache ? SpConfig.themeType() : themeType;
    if (type == 0) {
      return ThemeMode.light;
    } else if (type == 1) {
      return ThemeMode.dark;
    } else if (type == 2) {
      return ThemeMode.system;
    }
  }

  configLocalThemeData({BuildContext? context}) {
    int themeType = SpConfig.themeType();
    if (themeType == 0) {
      setLightTheme();
    } else if (themeType == 1) {
      setDarkTheme();
    } else {
      if (context != null) {
        if (MediaQuery.of(context).platformBrightness == Brightness.dark) {
          setDarkTheme();
        } else {
          setLightTheme();
        }
      }
    }
  }

  SystemUiOverlayStyle configIosSystemUiOverlayStyle() {
    return !isDarkMode ? SystemUiOverlayStyle.dark : SystemUiOverlayStyle.light;
  }

  // 配置状态栏和导航栏的颜色
  SystemUiOverlayStyle configSystemUiOverlayStyle({
    Color statusBarColor = const Color(0xff181818),
    Color systemNavigationBarColor = const Color(0xff181818),
    Brightness? brightness,
    bool noChange = false,
  }) {
    if (kIsWeb) return configIosSystemUiOverlayStyle();

    SystemUiOverlayStyle value;
    if (Platform.isAndroid) {
      value = SystemUiOverlayStyle(
        statusBarColor: statusBarColor,

        /// 安卓系统状态栏存在底色，所以需要加这个
        systemNavigationBarColor: systemNavigationBarColor,
        systemNavigationBarIconBrightness: brightness ?? configBrightness(),
        statusBarIconBrightness: brightness ?? configBrightness(),

        /// 状态栏字体颜色
        statusBarBrightness:
            brightness ?? (noChange ? Brightness.light : configBrightness()),
      );
    } else {
      ///iOS设置状态栏的正确姿势
      value = noChange
          ? SystemUiOverlayStyle.light
          : configIosSystemUiOverlayStyle();
    }
    return value;
  }

  Brightness configBrightness() {
    return isDarkMode ? Brightness.light : Brightness.dark;
  }
}
